#!/user/bin/env python
# -*- coding:UTF-8 -*-
'''
'''



import win32com.client
from time import sleep
import sys, time
import pythoncom
import threading
import re

#1、加入多线程
#2、加入异常处理
#3、加入读写文件

stopEvent=threading.Event()
class EventSink(object): 
    def OnNavigateComplete2(self,*args):
        stopEvent.set()
#wait for ie ok
def waitUntilReady(ie):
    if ie.ReadyState!=4:
        while 1:
            #print("waiting")
            pythoncom.PumpWaitingMessages()
            stopEvent.wait(.2)
            if stopEvent.isSet() or ie.ReadyState==4:
                stopEvent.clear()
                break;

#login_url = 'https://login.live.com/login.srf?wa=wsignin1.0&rpsnv=11&ct=1336294672&rver=6.0.5286.0&wp=MBI&wreply=https:%2F%2Flive.xbox.com:443%2Fxweb%2Flive%2Fpassport%2FsetCookies.ashx%3Frru%3Dhttps%253a%252f%252flive.xbox.com%252fen-US%252fAccount%252fSignin%253freturnUrl%253dhttp%25253a%25252f%25252fwww.xbox.com%25252fen-US%25252f%25253flc%25253d1033&lc=1033&id=66262&cbcxt=0'

login_url='https://login.live.com'
logout_url = 'https://live.xbox.com/Account/Signout'

#username='natedogg_w@yahoo.com'
#password='w12345'
username='samcool345@hotmail.co.uk'
password='killer1'


ie = win32com.client.Dispatch("InternetExplorer.Application")
ie.Visible = 1

first_url="http://www.xbox.com"

#first_url
ie.Navigate(first_url)
waitUntilReady(ie)
sleep(5)

#second_url,login
print("open login page")
second_url="https://live.xbox.com/Account/Signin?returnUrl=http%3a%2f%2fwww.xbox.com%2fen-US%2f"
ie.Navigate(second_url)
#while ie.Busy:
#    sleep(1)

waitUntilReady(ie)
#sleep(20)
# log in
#print("open login page")
#ie.Navigate(login_url)
#while ie.Busy:
#    sleep(1)


print("start login")
ie.Document.getElementById("i0116").value = username
ie.Document.getElementById("i0118").value = password
ie.Document.getElementById("idSIButton9").click()
waitUntilReady(ie)
sleep(60)
print("login success")

#如何判断有没有登陆成功？

# check account informations
#account_url = 'https://live.xbox.com/en-US/Account?xr=mebarnav'
#account_url = 'http://www.xbox.com'
#ie.Navigate(account_url)
#while ie.Busy:
#    sleep(1)


#again sign in
#ie.Navigate(second_usrl)
#while ie.Busy:
#    sleep(1)


#sleep(30);
#balance = str(ie.Document.getElementsById('points')[0].innerText)
balance = str(ie.Document.getElementById('points').firstChild.innerText)
# is bound or not



#print("%s %s %s %s %s %s" % (username, password, balance,bound, pp, country))
print("checking account informations")
ie.Document.getElementById('points').nextSibling.firstChild.click()
state_url = ie.Document.getElementById('points').nextSibling.firstChild.href
print(state_url)
regex=r"com/(.+)/Account"
country=re.search(regex,state_url).groups()[0];
print(country)
waitUntilReady(ie)
sleep(30)

#获得余额
#nodes=ie.Document.getElementById('PointsBalance').childNodes
#balance=nodes[2].firstChild
PP_AND_CARD=ie.Document.getElementById("BodyContent").innerText
#print(PP_AND_CARD)

regex=r"MasterCard:"
bound = re.search(regex,PP_AND_CARD);
regex=r"PayPal:"
pp = re.search(regex,PP_AND_CARD);

if bound:
    bound = '有绑定'
else:
    bound = '无绑定'
if pp:
    pp = '有pp'
else:
    pp = '无pp'
# country
#country = "USA"

print("%s %s %s %s %s %s" % (username, password, balance,bound, pp, country))

# log out
#ie.Navigate(logout_url)
#while ie.Busy:
#    sleep(1)
#print("log out successfully")

